package web;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by taohj on 2014/12/19.
 */
@WebListener
public class MyHttpSessionListener  implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

       // httpSessionEvent.getSession().setMaxInactiveInterval(3);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"----sessionCreated");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

       String filepath= httpSessionEvent.getSession().getAttribute("filepath")+"";
        System.out.println("filepath:"+filepath);
        if (filepath!=null && filepath.length()>0){
            File file=new File(filepath);
           if (file.exists()){
               file.delete();
           }
        }

        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"----sessionDestroyed");

    }
}
